#-------------------------------------------------------------------------------
# Name:        World
# Purpose:
#
# Author:      Pablo
#
# Created:     11/01/2013
# Copyright:   (c) Monxcleyr Productions 2013
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, os, logging
from pygame.color import *
import imagehandler, objects, tile, station

class World():

  def __init__(self, surface):
    self.imagehandler = imagehandler.ImageHandler()
    self.camerapos = (0,0)
    self.runningtime = 0

    #Create player
    self.playerspawn = (512, 390)
    sprite = 'player.png'
    self.player = objects.Player(self.playerspawn, sprite, self.imagehandler)

    self.createShip(surface)


    #Create stations
    self.stations = station.StationFactory()

    self.stations.addHealthStation((656,378), 'healthstation.png', self.imagehandler, (40,40), 'health', True)
    self.stations.addLifeSupportStation((368,378), 'lifesupportstation.png', self.imagehandler, (40,40), 'life support', True)
    self.stations.addNavigationStation((512,100), 'navstation.png', self.imagehandler, (40,40), 'navigation', True)
    self.stations.addAIStation((512,297), 'aistation.png', self.imagehandler, (40,40), 'ai', True)
    self.stations.addPowerStation((566,160), 'powerstation.png', self.imagehandler, (40,40), 'power', True)
    self.stations.addBreachEntity((550,259), 'breach.png', self.imagehandler, (40,40), 'breach1', False)
    self.stations.addBreachEntity((458,100), 'breach.png', self.imagehandler, (40,40), 'breach2', False)
    self.stations.addLeakEntity((430,307), 'leak.png', self.imagehandler, (40,40), 'leak1', False)






  def createShip(self, surface):
    self.loadShip(surface)

    self.tilelist = []
    for coord in self.tilecoords:
      self.tilelist.append(tile.Tile(coord, (16,16)))


  def loadShip(self, surface):
    baseimage = self.imagehandler.getSprite('shipborder.png')
    colorlist = []

    self.shipsize = (baseimage.get_width() * 16, baseimage.get_height() * 16)
    width = ((surface.get_width() - self.shipsize[0]) / 2)
    height = ((surface.get_height() - self.shipsize[1]) / 2)

    rect = pygame.Rect((512, 288), self.shipsize)
    rect.center = (512, 288)
    self.shippos = rect.topleft

    for row in range(0,baseimage.get_height()):
      for column in range(0,baseimage.get_width()):
        temp = baseimage.get_at((column, row))
        temp = ((column, row), (temp[0],temp[1],temp[2]))
        colorlist.append(temp)

    self.tilecoords = []
    for i in range(0, len(colorlist)):
      if colorlist[i][1] == (0,0,0):
        self.tilecoords.append(((colorlist[i][0][0] * 16) + width, (colorlist[i][0][1] * 16) + height))


  def updateCollisions(self):
    for tile in self.tilelist:
      if self.player.rect.colliderect(tile.rect):
        self.satCollision(self.player, tile)


  def satCollision(self, entity, tile):

    """ This calculates the actually collisions between two rects.
    Takes a sprite instance and a map.solid key (x,y)"""

    x1 = entity.worldpos[0]
    y1 = entity.worldpos[1]
    w1 = entity.rect.width
    h1 = entity.rect.height
    x2 = tile.worldpos[0]
    y2 = tile.worldpos[1]
    h2 = tile.rect.width
    w2 = tile.rect.height
    project = []

    if x1 + w1 > x2 + w2:
      dx = x1 + w1 - x2
      project.append('left') #1
    else:
      dx = x2 + w2 - x1
      project.append('right') #2
    if y1 + h1 > y2 + h2:
      dy = y1 + h1 - y2
      project.append('up') #11
    else:
      dy = y2 + h2 - y1
      project.append('down') #12
    if abs(dx) > abs(dy):
      entity.allowed = False
      if project[0] == 'left':
        entity.worldpos = (x2 + w2, entity.worldpos[1])
      else:
        entity.worldpos = (x2 - w1, entity.worldpos[1])
    if abs(dx) <= abs(dy):
      if project[1] == 'down':
        entity.worldpos = (entity.worldpos[0], y2 - h1)
      else:
        entity.worldpos = (entity.worldpos[0], y2 + h2)

    entity.rect = pygame.Rect(entity.worldpos, entity.framesize)